/**
 * 
 */
package src.sil4.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.torque.util.Criteria;

//import src.sil4.model.Etudiant;
import src.sil4.model.GestionFactory;

import fr.iut2.tc4.aideprojet2.torque.generated.*;


/**
 * @author KingJackson
 *
 */
public class Controller  extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String urlEditNote;
	private String urlConsNote;
	private String urlEditAbsence;
	private String urlConsAbsence;
	private String urlTest;
	private static String atGroupeChoisit;
	
	// INIT
	public void init() throws ServletException {
		urlEditAbsence = getServletConfig().getInitParameter("urlEditAbsence");
		urlConsNote = getServletConfig().getInitParameter("urlConsNote");
		urlEditNote = getServletConfig().getInitParameter("urlEditNote");
		urlConsAbsence = getServletConfig().getInitParameter("urlConsAbsence");
		urlTest = getServletConfig().getInitParameter("urlTest");
		Controller.atGroupeChoisit = "all";      
	}
	
	// POST
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		// on passe la main au GET
		doGet(request, response);
	}
	
	// GET
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {


		// On récupère la méthode d'envoi de la requête
		String methode = request.getMethod().toLowerCase();
		
		// On récupère l'action à exécuter
		String action = request.getPathInfo();
		if (action == null) {
			action = "/consulationNote.jsp";
		}
		
		// Exécution action
		if (methode.equals("get") && action.equals("/consultationNote.jsp") || methode.equals("post") && action.equals("/consultationNote.jsp")) {

			String param = request.getParameter("groupeChoisit");
			param = (param==null) ? "" : param;
			
			System.out.println("param: "+param );
			

			if(param.equals("") || param.equals("all")){
				
				try {
					request.setAttribute("groupe",EtudiantPeer.doSelectAll());
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}else {
				
				try {
					
					request.setAttribute("groupe",EtudiantPeer.doSelectWithGroupParam(param));
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			request.setAttribute("groupeChoisit",param);
			request.setAttribute("groupes", GestionFactory.getGroups());
			
			redirect(urlConsNote,request, response);
			
			
		} 
		// Exécution action
		else if (methode.equals("get") && action.equals("/consultationAbsence.jsp") || methode.equals("post") && action.equals("/consultationAbsence.jsp")){

			String param = request.getParameter("groupeChoisit");
			param = (param==null) ? "" : param;
			
			System.out.println("param: "+param );
			

			if(param.equals("") || param.equals("all")){
				
				try {
					request.setAttribute("groupe",EtudiantPeer.doSelectAll());
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}else {
				
				try {
					
					request.setAttribute("groupe",EtudiantPeer.doSelectWithGroupParam(param));
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			request.setAttribute("groupeChoisit",param);
			request.setAttribute("groupes", GestionFactory.getGroups());
			
			redirect(urlConsAbsence,request, response);
		}
		else if (methode.equals("get") && action.equals("/editionAbsence.jsp") ||methode.equals("post") && action.equals("/editionAbsence.jsp") ) {
			
			String param = request.getParameter("groupeChoisit");
			param = (param==null) ? "" : param;
			
			System.out.println("iiiiffff");
			String [] tab = new String [GestionFactory.getEtudiantsParGroupe("all").size()];
			int i=0;

			if(param.equals("") || param.equals("all")){
				request.setAttribute("groupe",GestionFactory.getEtudiantsParGroupe("all"));
			
				//System.out.println("iiiiffff");
			
				List<Etudiant> liste;
				try {
					liste = EtudiantPeer.doSelectAll();
					
					for(Etudiant e : liste){
						if(request.getParameter("nbAb-"+e.getEtudiantid())!=null){
							tab[i] = request.getParameter("nbAb-"+e.getEtudiantid());
							request.setAttribute("groupeChoisit","all");
							System.out.println("etudiant :"+e.getEtudiantid()+" nbAb :"+ tab[i]);
							
							System.out.println("nb abs en + :" + Integer.parseInt(tab[i]));
							e.setNbabsence(e.getNbabsence()+Integer.parseInt(tab[i]));
							System.out.println("result :" + e.getNbabsence());
							e.save();
						}
					}
					request.setAttribute("groupe",liste);
					
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
				
			}else {
				
				List<Etudiant> liste;
				try {
					liste = EtudiantPeer.doSelectWithGroupParam(param);
					
					for(Etudiant e : liste){
						if(request.getParameter("nbAb-"+e.getEtudiantid())!=null){
							tab[i] = request.getParameter("nbAb-"+e.getEtudiantid());
							request.setAttribute("groupeChoisit","all");
							System.out.println("etudiant :"+e.getEtudiantid()+" nbAb :"+ tab[i]);
							
							//GestionFactory.getEtudiants().get(e.getEtudiantid())
									//.setNbAbsences(Integer.parseInt(tab[i])+ e.getNbabsence()  );
							
							//EtudiantPeer.retrieveByPK(e.getEtudiantid()).setNbabsence(e.getNbabsence()+Integer.parseInt(tab[i]));
							
							e.setNbabsence(e.getNbabsence()+Integer.parseInt(tab[i]));
							e.save();
						
						}
					}
					
					request.setAttribute("groupe",liste);
					
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			request.setAttribute("groupeChoisit",param);
			request.setAttribute("groupes",  GestionFactory.getGroups());
			redirect(urlEditAbsence,request, response);
		}
		else if (methode.equals("get") && action.equals("/editionNote.jsp")) {
			
			redirect(urlEditNote,request, response);
		}
		else if (methode.equals("get") && action.equals("/test.jsp")){
			
		
			ArrayList<Etudiant> listeEtu= new ArrayList<Etudiant>();
			try {
				listeEtu = new ArrayList<Etudiant>(EtudiantPeer.doSelectAll());
				
				
				
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} 

			request.setAttribute("listeEtu",listeEtu);
			redirect(urlTest,request,response);
		}
			
		else {
			redirect(action,request, response);
		}
	}

	//
	private void redirect(String url,HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		loadJSP(url, request, response);
	}


	public void loadJSP(String url, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		ServletContext sc = getServletContext();
		RequestDispatcher rd = sc.getRequestDispatcher(url);
		rd.forward(request, response);
	}
}
